package com.dqhl.business.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import com.dqhl.business.Base.BaseActivity;
import com.dqhl.business.R;
import com.dqhl.business.fragment.ManagementFragment;
import com.dqhl.business.fragment.OrderFragment;
import com.dqhl.business.fragment.PersonalFragment;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private boolean quit = false; //设置退出标识

    private static final int ORDER = 0;
    private static final int MANAGEMENT = 1;
    private static final int PERSONAL = 2;

    private ImageView iv_bottom_order;
    private ImageView iv_bottom_management;
    private ImageView iv_bottom_personal;

    private OrderFragment orderFragment;
    private ManagementFragment managementFragment;
    private PersonalFragment personalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        showFragment(ORDER);
    }

    private void initView() {
        iv_bottom_order = (ImageView) findViewById(R.id.iv_bottom_order);
        iv_bottom_management = (ImageView) findViewById(R.id.iv_bottom_management);
        iv_bottom_personal = (ImageView) findViewById(R.id.iv_bottom_personal);

        iv_bottom_order.setOnClickListener(this);
        iv_bottom_management.setOnClickListener(this);
        iv_bottom_personal.setOnClickListener(this);
    }

    private void showFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragment(ft);
        initBottomIcon();
        switch (index) {
            case ORDER:
                if (orderFragment != null) {
                    ft.show(orderFragment);
                } else {
                    orderFragment = new OrderFragment();
                    ft.add(R.id.flame_layout, orderFragment, "OrderFragment");
                }
                iv_bottom_order.setImageResource(R.drawable.ic_bottom_order_selected);
                break;
            case MANAGEMENT:
                if (managementFragment != null) {
                    ft.show(managementFragment);
                } else {
                    managementFragment = new ManagementFragment();
                    ft.add(R.id.flame_layout, managementFragment, "ManagementFragment");
                }
                iv_bottom_management.setImageResource(R.drawable.ic_bottom_management_selected);
                break;
            case PERSONAL:
                if (personalFragment != null) {
                    ft.show(personalFragment);
                } else {
                    personalFragment = new PersonalFragment();
                    ft.add(R.id.flame_layout, personalFragment, "PersonalFragment");
                }
                iv_bottom_personal.setImageResource(R.drawable.ic_bottom_personal_selected);
                break;
        }
        ft.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (orderFragment != null) {
            ft.hide(orderFragment);
        }
        if (managementFragment != null) {
            ft.hide(managementFragment);
        }
        if (personalFragment != null) {
            ft.hide(personalFragment);
        }
    }

    private void initBottomIcon() {
        iv_bottom_order.setImageResource(R.drawable.ic_bottom_order);
        iv_bottom_management.setImageResource(R.drawable.ic_bottom_management);
        iv_bottom_personal.setImageResource(R.drawable.ic_bottom_personal);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_bottom_order:
                showFragment(ORDER);
                break;
            case R.id.iv_bottom_management:
                showFragment(MANAGEMENT);
                break;
            case R.id.iv_bottom_personal:
                showFragment(PERSONAL);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (!quit) {
            toast("再按一次退出程序");
            new Timer(true).schedule(new TimerTask() {
                @Override
                public void run() {
                    quit = false;
                }
            }, 2000);
            quit = true;
        } else {
            super.onBackPressed();
            finish();
        }
    }
}
